package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.couponGift.CouponGiftBean;
import com.burgeon.backend.SHOPBACKEND.entity.couponGift.CouponGiftItem;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendCouponGiftMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendCouponGiftService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import com.burgeon.backend.service.CouponService;
import com.burgeon.backend.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class BackendCouponGiftServiceImpl implements BackendCouponGiftService {

    @Autowired
    private BackendCouponGiftMapper backendCouponGiftMapper;
    @Autowired
    private CouponService couponService;

    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(8);

    private void createCouponThread(List<CouponGiftItem> itmeList,String phone,String account){


    // 2. 创建好Runnable类线程对象 & 需执行的任务
        for (CouponGiftItem couponGiftItem : itmeList){
            for (int i = 0;i<couponGiftItem.getCount();i++){
                Runnable task = () -> {
                    System.out.println("执行任务啦");
                    try {

                        couponService.addCoupon(couponGiftItem.getTid(),phone,account);

                        System.err.println(123);
                    } catch (InsertException e) {
                        e.printStackTrace();
                        System.err.println(e.getMessage());

                    }
                };
                // 3. 向线程池提交任务
                fixedThreadPool.execute(task);

            }
        }

    }



    @Override
    public void createCoupon(String id,String phone,String account) throws ErrorDataException {
        List<CouponGiftItem> itmeList = getItmeList(id);


        createCouponThread(itmeList,phone,account);


    }

    @Override
    public void delCouponGiftItem(String id) {
        backendCouponGiftMapper.delItem(id);
    }

    @Override
    public void addCouponGift(String name,  String account) {
        CouponGiftBean couponGiftBean = new CouponGiftBean();
        couponGiftBean.setName(name);
        couponGiftBean.setToken(account);
        backendCouponGiftMapper.addCouponGift(couponGiftBean);



    }

    @Override
    public void addCouponGiftItem(String cid, String[] tids) {
        for (String tid:tids){
            backendCouponGiftMapper.addCouponGiftItem(cid,tid);
        }

    }

    @Override
    public List<CouponGiftBean> getList(String account) {
        return backendCouponGiftMapper.getlist(account);
    }

    @Override
    public List<CouponGiftItem> getItmeList(String id) {
        return backendCouponGiftMapper.getItemList(id);
    }

    @Override
    public void upItemCount(String id, int count) {
        backendCouponGiftMapper.upItemCount(id,count);
    }

    @Override
    public void upContent(String cid, String name, Integer integral, String startDate, String endDate, String show) {
        backendCouponGiftMapper.upContent(cid,name,integral,startDate,endDate,show);
    }

    @Override
    public CouponGiftBean showContent(String cid) {
        return backendCouponGiftMapper.showContent(cid);
    }
}
